﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace Turners.DMS.WebParts
{
    public partial class SearchReceiptsWebPartUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                BranchDropDownDataBind();
        }
        //Load branches from Lists/Branches list and bind to dropdown list
        private void BranchDropDownDataBind()
        {
            SPSite site = SPContext.Current.Site;
            using (SPSite siteCollection = new SPSite(site.ID))
            {
                using (SPWeb root = siteCollection.RootWeb)
                {
                    SPList branches = root.Lists["Branches"];
                    List<String> branchList = new List<string>();
                    branchList.Add("");
                    foreach (SPListItem item in branches.Items)
                    {
                        branchList.Add(item["Title"].ToString());
                    }
                    BranchDropDown.DataSource = branchList;
                    BranchDropDown.DataBind();
                }
            }
        }


        protected void SearchButton_Click(object sender, EventArgs e)
        {
            using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb homeWeb = currentSite.AllWebs["Search"])
                {
                    string searchResultsPageUrl = homeWeb.Url + "/resultsreceipt.aspx?k=";

                    if (SearchAllTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += SearchAllTextBox.Text;
                    }
                    if (UsernameTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " UserFullName:\"" + UsernameTextBox.Text + "\"";
                    }

                    if (DateStartTextBox.Text == String.Empty || DateEndTextBox.Text == String.Empty)
                    {
                        if (DateStartTextBox.Text != String.Empty)
                        {
                            string reformattedDate = DateStartTextBox.Text.Split('/')[1] + "/" + DateStartTextBox.Text.Split('/')[0] + "/" + DateStartTextBox.Text.Split('/')[2];
                            searchResultsPageUrl += " ReceiptDate>\"" + reformattedDate + "\"";
                        }
                        if (DateEndTextBox.Text != String.Empty)
                        {
                            string reformattedDate = DateEndTextBox.Text.Split('/')[1] + "/" + DateEndTextBox.Text.Split('/')[0] + "/" + DateEndTextBox.Text.Split('/')[2];
                            searchResultsPageUrl += " ReceiptDate<\"" + reformattedDate + "\"";
                        }
                    }else
                    {
                        string reformattedstartDate = DateStartTextBox.Text.Split('/')[1] + "/" + DateStartTextBox.Text.Split('/')[0] + "/" + DateStartTextBox.Text.Split('/')[2];
                        string reformattedendDate = DateEndTextBox.Text.Split('/')[1] + "/" + DateEndTextBox.Text.Split('/')[0] + "/" + DateEndTextBox.Text.Split('/')[2];

                        searchResultsPageUrl += " (ReceiptDate>\"" + reformattedstartDate + "\" AND ReceiptDate<\"" + reformattedendDate + "\")";
                        
                    }


                    if (InvoiceTypeCodeDropDown.SelectedValue != String.Empty)
                    {
                        searchResultsPageUrl += " InvoiceType:\"" + InvoiceTypeCodeDropDown.SelectedValue + "\"";
                    }
                    if (InvoiceNumberTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " InvoiceNumber:\"" + InvoiceNumberTextBox.Text + "\"";
                    }
                    if (CustomerNumberTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " CustomerNumber:\"" + CustomerNumberTextBox.Text + "\"";
                    }
                    if (CustomerNameTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " CustomerName:\"" + CustomerNameTextBox.Text + "\"";
                    }
                    if (BranchDropDown.SelectedValue != String.Empty)
                    {
                        searchResultsPageUrl += " BranchName:\"" + BranchDropDown.SelectedValue + "\"";
                    }
                    if (BidderNoTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " BidderNo:\"" + BidderNoTextBox.Text + "\"";
                    }


                    searchResultsPageUrl += "&r=fileextension%3D%22aspx%22";

                    Response.Redirect(searchResultsPageUrl);
                }

            }

        }
    }
}
